package com.chinackts.exceptionadvice.listener;

import com.chinackts.exceptionadvice.event.SingleWindowCollectionErrorEvent;
import com.chinackts.exceptionadvice.support.dingding.SendDingForMD;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author Lianhong
 * @since 2021/2/5
 */
@EnableAsync
@Component
public class SendSingleWindowCollectionErrorMessage {

    @Resource
    private SendDingForMD sendDingForMD;

    @Value("${ding.enable}")
    private boolean dingEnabled;

    @EventListener
    public void sendMessage(SingleWindowCollectionErrorEvent event) throws IOException {
        if (dingEnabled) sendDingForMD.send(event.getTitle(), event.getMessage());
    }

}